package models

import (
	"time"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

//只能根据出发地和目的地查询
//Search2最后的数字表示带两个参数
func Search2(source, destination string) ([]*Aline, error) {
	o := orm.NewOrm()
	alines := make([]*Aline, 0)
	qs := o.QueryTable("aline")
	_, err := qs.Filter("source", source).Filter("destination", destination).All(&alines)
	return alines, err
}

// 根据出发地和目的地和出发时间查询
func Search3(source, destination string, dateTime time.Time) ([]*Aline, error) {
	o := orm.NewOrm()
	alines := make([]*Aline, 0)
	qs := o.QueryTable("aline")
	year := dateTime.Year()
	month := dateTime.Month()
	day := dateTime.Day()
	_, err := qs.Filter("source", source).Filter("destination", destination).
		Filter("year", year).Filter("month", month).Filter("day", day).All(&alines)
	return alines, err
}

// 查询全部的出发地址
// 参数返回：
// 0：返回正常
// 1：系统错误
func Listsource() ([]string, int8) {
	o := orm.NewOrm()
	var lists orm.ParamsList
	num, err := o.Raw("SELECT DISTINCT source FROM aline").ValuesFlat(&lists)
	sources := make([]string, num)
	if num > 0 && err == nil {
		for pos, aline := range lists {
			sources[pos] = aline.(string)
		}
	} else {
		return nil, 1
	}

	// alines := make([]*Aline, 0)
	// qs := o.QueryTable("aline")
	// size, err := qs.Distinct().All(&alines)
	// sources := make([]string, size)
	// if err != nil {
	// 	return nil, 1
	// }
	// for pos, aline := range alines {
	// 	sources[pos] = aline.Source
	// }
	return sources, 0
}

// 查询全部的目的地址
// 参数返回：
// 0：返回正常
// 1：系统错误
func Listdest() ([]string, int8) {
	o := orm.NewOrm()

	var lists orm.ParamsList
	num, err := o.Raw("SELECT DISTINCT destination FROM aline").ValuesFlat(&lists)
	sources := make([]string, num)
	if num > 0 && err == nil {
		for pos, aline := range lists {
			sources[pos] = aline.(string)
		}
	} else {
		return nil, 1
	}

	// alines := make([]*Aline, 0)
	// qs := o.QueryTable("aline")
	// size, err := qs.Distinct().All(&alines)
	// sources := make([]string, size)
	// if err != nil {
	// 	return nil, 1
	// }
	// for pos, aline := range alines {
	// 	sources[pos] = aline.Destination
	// }
	return sources, 0
}

// 检查航班信息并把信息发送给用户
func Sendmess(o orm.Ormer) {
	qs := o.QueryTable("aline")
	alines := make([]*Aline, 0)
	// _, err := qs.Filter("status", 0).Exclude(&alines)
	_, err := qs.Exclude("status", 0).All(&alines)
	if err != nil {
		beego.Error(err)
	}
	for _, aline := range alines {
		orders := make([]*Order, 0)
		qsorder := o.QueryTable("order")
		_, err = qsorder.Filter("alineid", aline.Id).All(&orders)
		if err != nil {
			beego.Error(err)
		}
		for _, order := range orders {
			users := make([]*User, 0)
			qsuser := o.QueryTable("user")
			_, err = qsuser.Filter("id", order.Userid).All(&users)
			if err != nil {
				beego.Error(err)
			}
			for _, user := range users {
				mess := &Usermessage{
					Userid:  user.Id,
					Title:   "航班通知",
					Content: aline.Statusdetail,
				}
				_, err = o.Insert(mess)
				if err != nil {
					beego.Error(err)
				}
			}

		}

	}
	return
}
